package com.xhtt.modules.app.controller.inspection;

import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.R;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.app.annotation.LoginUser;
import com.xhtt.modules.inspection.dto.InspectionAppDTO;
import com.xhtt.modules.inspection.enums.InspectTypeEnum;
import com.xhtt.modules.inspection.service.IInsTaskService;
import com.xhtt.modules.inspection.service.IInsTaskStepService;
import com.xhtt.modules.inspection.vo.InspectionAppVO;
import com.xhtt.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 巡检管理-现场巡检
 * </p>
 *
 * @author xj
 * @since 2019-01-29
 */
@RestController
@RequestMapping("app/inspection")
@Api(value = "巡检管理-现场巡检", tags = "巡检管理-现场巡检")
public class InsTaskAppController {

    @Autowired
    private IInsTaskStepService insTaskStepService;

    @Autowired
    private IInsTaskService insTaskService;

    /**
     * @see InspectTypeEnum
     */
    @GetMapping(value = "/list/{inspectType}")
    @ApiOperation("巡检管理-现场巡检-获取现场巡检列表(app)")
    @Login
    public R list(@PathVariable("inspectType") Integer inspectType, @ApiIgnore @LoginUser SysUserEntity user) {
        List<InspectionAppDTO> inspectionAppDTOS = new ArrayList<>();
        if (inspectType.equals(InspectTypeEnum.DAY.getCode())) {
            inspectionAppDTOS = insTaskStepService.queryDayList(user.getUserId().intValue());
        } else if (inspectType.equals(InspectTypeEnum.WEEK.getCode())) {
            inspectionAppDTOS = insTaskStepService.queryWeekList(user.getUserId().intValue());
        } else if (inspectType.equals(InspectTypeEnum.MONTH.getCode())) {
            inspectionAppDTOS = insTaskStepService.queryMonthList(user.getUserId().intValue());
        }
        return R.ok().put(Constant.DATA, inspectionAppDTOS);
    }

    @GetMapping(value = "/list")
    @ApiOperation("巡检管理-现场巡检-获取现场巡检任务数量(app)")
    @Login
    public R list(@ApiIgnore @LoginUser SysUserEntity user) {
        int count = 0;
        List<InspectionAppDTO> dayList = insTaskStepService.queryDayList(user.getUserId().intValue());
        if (CollectionUtils.isNotEmpty(dayList)) {
            count += dayList.size();
        }
        List<InspectionAppDTO> weekList = insTaskStepService.queryWeekList(user.getUserId().intValue());
        if (CollectionUtils.isNotEmpty(weekList)) {
            count += weekList.size();
        }
        List<InspectionAppDTO> monthList = insTaskStepService.queryMonthList(user.getUserId().intValue());
        if (CollectionUtils.isNotEmpty(monthList)) {
            count += monthList.size();
        }
        return R.ok().put(Constant.DATA, count);
    }


    @GetMapping(value = "/getPointDetail")
    @ApiOperation("巡检管理-现场巡检-获取检测点详情(app)")
    @Login
    public R getPointDetail(@RequestParam("taskId") Integer taskId, @RequestParam("stepId") Integer stepId, @ApiIgnore @LoginUser SysUserEntity user) {
        return R.ok().put(Constant.DATA, insTaskService.getAllPoint(taskId, stepId, user));
    }

    @PostMapping(value = "/commit")
    @ApiOperation("巡检管理-现场巡检-确认提交")
    @Login
    public R commit(@RequestBody InspectionAppVO inspectionAppVO, @ApiIgnore @LoginUser SysUserEntity user) {
        insTaskService.commit(inspectionAppVO, user);
        return R.ok();
    }


}
